import type { TypeStatus, PicLink } from '@/types';
import { isPicLink, isValueIndex } from '@/types';
import { message } from 'antd';
// 切换标题和描述的编辑内容
export function setType(status: TypeStatus, payload: number) {
  if (payload !== status.type.currentStatus) {
    status.title.isShow = !status.title.isShow;
    status.desc.isShow = !status.desc.isShow;
    status.position.isShow = !status.position.isShow;
    status.titleSize.isShow = !status.titleSize.isShow;
    status.descSize.isShow = !status.descSize.isShow;
    status.titleWeight.isShow = !status.titleWeight.isShow;
    status.descWeight.isShow = !status.descWeight.isShow;
    status.titleItalic.isShow = !status.titleItalic.isShow;
    status.descItalic.isShow = !status.descItalic.isShow;
    status.titleColor.isShow = !status.titleColor.isShow;
    status.descColor.isShow = !status.descColor.isShow;
  }
}

export function dispatchStatus(
  // store: EditorStore | MaterialStore,
  // status: TypeStatus | OptionsStatusProps,
  store: any,
  status: any,
  configKey: string,
  payload?: number | string | boolean | object,
  isShowChange?: boolean
) {
  if (['title', 'desc'].includes(configKey)) {
    if (typeof payload !== 'string') {
      return console.error('Invalid payload type for "title or desc". Expected string.');
    }
    store.setTextStatus(status[configKey], payload);
  }
  if (['options'].includes(configKey)) {
    if (typeof payload === 'number') {
      // 删除选项
      const res = store.rmOption(status[configKey], payload);
      if (res) {
        message.success('删除成功');
      } else {
        message.error('至少保留两个选项');
      }
    } else if (typeof payload === 'object') {
      store.setValueByIndex(status[configKey], payload);
      // if (isValueIndex(payload)) {
      //   store.setValueByIndex(status[configKey], payload);
      // }
      // if (isPicLink(payload)) {
      //   // 设置图片链接
      //   store.setPicLinkByIndex(status[configKey], payload as PicLink);
      // }
    } else {
      // 新增选项
      store.addOption(status[configKey]);
    }
  }
  if (['position'].includes(configKey)) {
    if (typeof payload !== 'number') {
      return console.error('Invalid payload');
    }
    store.setCurrentStatus(status[configKey], payload);
  }
  if (['titleSize', 'descSize'].includes(configKey)) {
    if (typeof payload !== 'number') {
      return console.error('Invalid payload');
    }
    store.setCurrentStatus(status[configKey], payload);
  }
  if (['titleWeight', 'descWeight'].includes(configKey)) {
    if (typeof payload !== 'number') {
      return console.error('Invalid payload');
    }
    store.setCurrentStatus(status[configKey], payload);
  }
  if (['titleItalic', 'descItalic'].includes(configKey)) {
    if (typeof payload !== 'number') {
      return console.error('Invalid payload');
    }
    store.setCurrentStatus(status[configKey], payload);
  }
  if (['titleColor', 'descColor'].includes(configKey)) {
    if (typeof payload !== 'string') {
      return console.error('Invalid payload type for "title or desc". Expected string.');
    }
    store.setTextStatus(status[configKey], payload);
  }

  if (['type'].includes(configKey)) {
    if (typeof payload !== 'number') {
      return console.error('Invalid payload');
    }
    setType(status, payload);
    store.setCurrentStatus(status[configKey], payload);
  }
}
